package com.primary.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.primary.common.Result;
import com.primary.controller.request.BloodbanksRequest;
import com.primary.entity.Bloodbanks;
import com.primary.entity.Users;
import com.primary.service.BloodbanksService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bloodbanks")
public class BloodbanksController {
    @Resource
    BloodbanksService bloodbanksService;

    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(BloodbanksRequest bloodbanksRequest){
        return Result.success(bloodbanksService.queryByPage(bloodbanksRequest));
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Bloodbanks bloodbanks) {
        if (bloodbanks.getDonors()!=null    &&
                bloodbanks.getBloodType()!=null) {
            return bloodbanksService.insertOne(bloodbanks) > 0 ?
                    Result.success() : Result.error("-114","插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody Bloodbanks bloodbanks){
        if (null != bloodbanks.getDonors() &&
                null != bloodbanks.getBloodInfo()){
            return bloodbanksService.updateOneById(bloodbanks) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
        return null == id ? Result.error("ID不能为空") :
                (bloodbanksService.deleteById(id) ? Result.success():Result.error("删除失败"));
    }

    @RequestMapping(value = "/allBloodbanks", method = {RequestMethod.GET})
    public Result allBloodbanks() {
        List<String> list = bloodbanksService.queryBloodbanksForSelecter();
        return Result.success(list);
    }

    @RequestMapping(value = "/charts", method = {RequestMethod.GET})
    public Result charts() {
        //查找所有的库存
        // 柱状图
        List<Bloodbanks> bloodbanksList1 = bloodbanksService.queryAllForBarAndPie();
        Set<String> bloodTypes=bloodbanksList1.stream().map(Bloodbanks::getBloodType).collect(Collectors.toSet());
        List<Dict> barList=new ArrayList<>();
        for(String bloodType : bloodTypes){
            BigDecimal sum=bloodbanksList1.stream().filter(blood -> blood.getBloodType().equals(bloodType)).map(Bloodbanks::getCounts)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict bar = dict.set("name",bloodType).set("value",sum);
            barList.add(bar);
        }
        // 折线图
        List<Bloodbanks> bloodbanksList = bloodbanksService.queryAll();
        Set<String> dates=bloodbanksList.stream().map(Bloodbanks::getInstoredate).collect(Collectors.toSet());
        List<String> dateList= CollUtil.newArrayList(dates);
        dateList.sort(Comparator.naturalOrder());
        List<Dict> lineList=new ArrayList<>();
        for(String date : dateList){
            BigDecimal sum=bloodbanksList.stream().filter(blood -> blood.getInstoredate().equals(date)).map(Bloodbanks::getCounts)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict line = dict.set("date",date).set("value",sum);
            lineList.add(line);
        }
        //综合数据
        Dict res=Dict.create().set("line",lineList).set("bar",barList);
        return Result.success(res);
    }
}
